<template>
	<view class="content">
		<view class="navbar">
			<view 
				v-for="(item, index) in navList" :key="index" 
				class="nav-item" 
				:class="{current: tabCurrentIndex === index}"
				@click="tabClick(index)"
			>
				{{item.text}}
			</view>
		</view>

		<swiper :current="tabCurrentIndex" class="swiper-box" duration="300" @change="changeTab">
			<swiper-item class="tab-content" v-for="(tabItem,tabIndex) in navList" :key="tabIndex">
				<scroll-view 
					class="list-scroll-content" 
					scroll-y
					@scrolltolower="loadData"
				>
					<!-- 空白页 -->
					<!-- <empty v-if="tabItem.loaded === true && tabItem.orderList.length === 0"></empty> -->
					<u-empty text="暂无订单" mode="list" v-if="tabItem.loaded === true && tabItem.orderList.length === 0"></u-empty>
					<!-- 订单列表 -->
					<view 
						v-for="(item,index) in tabItem.orderList" :key="index"
						class="order-item"
					>
						<view class="i-top b-b">
							<view class="tl">
								<view class="code">订单编号：{{item.order_id}}</view>
								<text class="time">{{item.createdate}}</text>
								<view>收货地址：{{item.address}}</view>
							</view>
							<text class="state" style="color:red" v-if="item.is_paid===0 && item.flow!=10 && item.flow!=9">待付款</text>
							<text class="state" style="color:red" v-if="item.is_paid===1 && item.flow===1">待发货</text>
							<text class="state" style="color:red" v-if="item.is_paid===1 && item.flow===2">已发货</text>
<!-- 							<text class="state" style="color:red" v-if="item.is_paid===1 && item.flow===7">拼团中</text>
							<text class="state" style="color:red" v-if="item.is_paid===1 && item.flow===8">已成团</text>
							<text class="state" style="color:red" v-if="item.is_paid===0 && item.flow===9">拼团失败</text>
							<text class="state" style="color:red" v-if="item.is_paid===0 && item.flow===10">拼团成功</text> -->
							<text 
								v-if="item.flow===6" 
								class="del-btn yticon icon-iconfontshanchu1"
								@click="deleteOrder(index)"
							></text>
						</view>
						
						
						<view 
							class="goods-box-single"
							v-for="(goodsItem,index) in item.goodsList" :key="index"
						>
							<!-- <image class="goods-img" :src="web+item.reward.img" v-if="item.reward&&item.reward!=0" mode="aspectFill"></image> -->
							<image class="goods-img" :src="goodsItem.img|imgfilter" mode="aspectFill"></image>
							<view class="right">
								<text class="title clamp">{{goodsItem.name}}</text>
								<text class="attr-box">厂家直发 x {{goodsItem.product_num}}</text>
								<text class="price">{{goodsItem.product_price}}</text>
							</view>
							<view class="action-box b-t" v-if="item.flow == 1 && goodsItem.product_id == 563 &&item.reward == 0">
								<button class="action-btn recom" @click="toreward(item.order_id)">点击拆盒</button>
							</view>
						</view>
						
						<view class="price-box">
							共
							<text class="num">{{item.goodsList.length}}</text>
							件商品 实付款
							<text class="price">{{item.product_price}}</text>
							
						</view>
						
						
						
						<view class="action-box b-t" v-if="item.is_paid != 1">
							<button class="action-btn" @click="cancelOrder(item,index)">取消订单</button>
							<button class="action-btn recom" @click="goPay(item.id)">立即支付</button>
							
						</view>
						
						<view class="action-box b-t" v-if="item.flow == 2">
							<!-- {{item.wuliu_info}} {{item.wuliu_code}} -->
							<button class="action-btn recom" @click="gowuliu(item.order_id)">查看物流</button>
							<!-- <button class="action-btn" @click="cancelOrder(item)"></button> -->
							<!-- <button class="action-btn recom" @click="getGoods(item)">确认收货</button> -->
						</view>
					</view>
					 
					<!-- <uni-load-more :status="tabItem.loadingType"></uni-load-more> -->
					<view class="loadmore">
						<u-loadmore :status="tabItem.loadingType" />
					</view>
				</scroll-view>
			</swiper-item>
		</swiper>
	
	</view>
</template> 

<script>
	
	// import Json from '@/Json';
	export default {
		
		data() {
			return {
				tabCurrentIndex: 0,
				navList: [{
						state: 0,
						text: '全部',
						loadingType: 'more',
						orderList: []
					},
					// {
					// 	state: 1,
					// 	text: '待付款',
					// 	loadingType: 'more',
					// 	orderList: []
					// },
					{
						state: 2,
						text: '待发货',
						loadingType: 'more',
						orderList: []
					},
					{
						state: 3,
						text: '已发货',
						loadingType: 'more',
						orderList: []
					},
					{
						state: 4,
						text: '售后',
						loadingType: 'more',
						orderList: []
					}
				],
				productlist:''
			};
		},
		
		// computed: {
		// 	// 计算属性的 getter
		// 	reversedMessage: function () {
		// 	  // `this` 指向 vm 实例
		// 	  return this.message.split('').reverse().join('')
		// 	}
		// }
	
		async onLoad(options){
			
			/**
			 * 修复app端点击除全部订单外的按钮进入时不加载数据的问题
			 * 替换onLoad下代码即可
			 */
			this.tabCurrentIndex = +options.state;
			// #ifndef MP
			//this.loadData()
			// #endif
			// #ifdef MP
			// if(options.state == 0){
			// 	this.loadData()
			// }
			// #endif
			var result = await this.$ajax.get('order/getrewardlist.html');
			this.productlist = result.data.data;
			console.log(result)
			
			var result = await this.$ajax.get('order/getList.html');
			this.navList[0].orderList = result.data.data;
			
			console.log(this.navList[0].orderList);
			
			if(options.state == 1) {
				var result = await this.$ajax.get('order/getListUnPay.html');
				this.navList[1].orderList = result.data.data;
			} else if(options.state == 2) {
				var result = await this.$ajax.get('order/getListPaidPay.html');
				this.navList[2].orderList = result.data.data;
			} else if(options.state == 3) {
				var result = await this.$ajax.get('order/getListGetGoods.html');
				this.navList[3].orderList = result.data.data;
			}
			
			//this.tabCurrentIndex = 0
		},
		
		filters: {
		    getSumPrice: function (value) {
				var sum = 0;
				
				for(var i = 0; i < value.length; i++) {
					sum += value[i].product_num * value[i].product_price
				}
				
				//console.log(value.product_num * value.);
				
				return sum
		    },
			imgfilter(str) {
				return str.replace("https","http")
			},
		   
		},
		  
		methods: {
					
			// 查询物流
			gowuliu(e){
				
				console.log(e,'555555555555')
				// e='16569705085831'
				uni.navigateTo({
					url:'/pages/order/wuliu?orderid='+e
				})
			},
			toreward(id){
				console.log(id);
				this.$u.route({
					url:'/pages/public/reward',
					type:"redirectTo",
					params:{
						id:id,
						productlist:JSON.stringify(this.productlist)
					}
				})
			},
			//获取订单列表
			loadData(source){
				//这里是将订单挂载到tab列表下
				let index = this.tabCurrentIndex;
				let navItem = this.navList[index];
				let state = navItem.state;
				
				if(source === 'tabChange' && navItem.loaded === true){
					//tab切换只有第一次需要加载数据
					return;
				}
				if(navItem.loadingType === 'loading'){
					//防止重复加载
					return;
				}
				
				navItem.loadingType = 'loading';
				
				setTimeout(()=>{
					let orderList = Json.orderList.filter(item=>{
						//添加不同状态下订单的表现形式
						item = Object.assign(item, this.orderStateExp(item.state));
						//演示数据所以自己进行状态筛选
						if(state === 0){
							//0为全部订单
							return item;
						}
						return item.state === state
					});
					orderList.forEach(item=>{
						navItem.orderList.push(item);
					})
					//loaded新字段用于表示数据加载完毕，如果为空可以显示空白页
					this.$set(navItem, 'loaded', true);
					
					//判断是否还有数据， 有改为 more， 没有改为noMore 
					navItem.loadingType = 'more';
				}, 600);	
			}, 

			//swiper 切换
			async changeTab(e){
				this.tabCurrentIndex = e.target.current;
				//this.loadData('tabChange');
				
				
			},
			//顶部tab点击
			async tabClick(index){
				this.tabCurrentIndex = index;
				console.log(index);
				
				
				
				//console.log(this.navList[0].orderList);
				
				if(index == 1) {
					var result = await this.$ajax.get('order/getListUnPay.html');
					this.navList[1].orderList = result.data.data;
				} else if(index == 2) {
					var result = await this.$ajax.get('order/getListPaidPay.html');
					this.navList[2].orderList = result.data.data;
				} else if(index == 3) {
					var result = await this.$ajax.get('order/getListGetGoods.html');
					this.navList[3].orderList = result.data.data;
				} else {
					var result = await this.$ajax.get('order/getList.html');
					this.navList[0].orderList = result.data.data;
				
					
				}
				
				// var result = await this.$ajax.get('order/getListUnPay.html');
				// this.navList[1].orderList = result.data.data;
				
				// var result = await this.$ajax.get('order/getListPaidPay.html');
				// this.navList[2].orderList = result.data.data;
				
				// var result = await this.$ajax.get('order/getListGetGoods.html');
				// this.navList[3].orderList = result.data.data;
			},
			
			async goPay(e) {
				uni.showModal({
					 title: '更新提示',
					 content: "当前商品请从首页购买",
					 success: (showResult) => {
						 if (showResult.confirm) {
							 uni.switchTab({
							 	url: '/pages/index/index',
							 })
						 } else {
							 //uni.navigateBack()
						 }
					 }
				})
				
				var result = (await this.$ajax.get('Product/checkBuy.html?id=' + e));
				
				console.log(result);
				return;
				
				if(result.data.code == 1) {
					
				} else {
					uni.showModal({
						 title: '更新提示',
						 content: "当前商品您只能购买一次",
						 success: (showResult) => {
							 if (showResult.confirm) {
								 uni.navigateBack()
							 } else {
								 uni.navigateBack()
							 }
						 }
					})
					return;
				}
				
				// if(result.data.code == -1) {
				// 	this.$api.tips(result.data.msg)
				// 	return;
				// }
				
				uni.redirectTo({
					url: '/pages/money/pay?id=' + e
				})
			},
			
			async getGoods(e) {
				var data = this.$api.copy(this.$data);
				
				data.navList = [];
				
				data.id = e.id;
				data.flow = 3;
				
				var result = await this.$ajax.get('order/setGoodsStatus.html',{data: data});
				
				this.$api.tips("订单已确认");
			},
			//删除订单
			deleteOrder(index){
				uni.showLoading({
					title: '请稍后'
				})
				setTimeout(()=>{
					this.navList[this.tabCurrentIndex].orderList.splice(index, 1);
					uni.hideLoading();
				}, 600)
			},
			//取消订单
			async cancelOrder(item,index){
				
				//console.log(item);
				//return;
				
				// uni.showLoading({
				// 	title: '请稍后'
				// })
				// setTimeout(()=>{
				// 	let {stateTip, stateTipColor} = this.orderStateExp(9);
				// 	item = Object.assign(item, {
				// 		state: 9,
				// 		stateTip, 
				// 		stateTipColor
				// 	})
					
				// 	//取消订单后删除待付款中该项
				// 	let list = this.navList[1].orderList;
				// 	let index = list.findIndex(val=>val.id === item.id);
				// 	index !== -1 && list.splice(index, 1);
					
				// 	uni.hideLoading();
				// }, 600)
				
				//return;
				
				// var data = this.$api.copy(this.$data);
				// data.navList = [];
				// data.id = item.id;
				// data.flow = 6;
				
				this.navList[this.tabCurrentIndex].orderList.splice(index, 1);
				
				// if(this.tabCurrentIndex == 0) {
				// 	var result = await this.$ajax.get('order/getListUnPay.html');
				// 	this.navList[this.tabCurrentIndex].orderList = result.data.data;
				// } else if(this.tabCurrentIndex == 1) {
				// 	var result = await this.$ajax.get('order/getListPaidPay.html');
				// 	this.navList[2].orderList = result.data.data;
				// } else if(this.tabCurrentIndex == 2) {
				// 	var result = await this.$ajax.get('order/getListGetGoods.html');
				// 	this.navList[3].orderList = result.data.data;
				// } else {
				// 	var result = await this.$ajax.get('order/getList.html');
				// 	this.navList[0].orderList = result.data.data;
				// }
				
				
				
				var result = await this.$ajax.get('order/setGoodsStatus.html',{data: item.id});
				
				this.$api.tips("订单已取消");
			},

			//订单状态文字和颜色
			orderStateExp(state){
				let stateTip = '',
					stateTipColor = '#fa436a';
				switch(+state){
					case 1:
						stateTip = '待付款'; break;
					case 2:
						stateTip = '待发货'; break;
					case 9:
						stateTip = '订单已关闭'; 
						stateTipColor = '#909399';
						break;
						
					//更多自定义
				}
				return {stateTip, stateTipColor};
			}
		},
	}
</script>

<style lang="scss">
	page, .content{
		background: #f8f8f8;
		height: 100%;
		-webkit-user-select: text; 
	}
	
	.swiper-box{
		height: calc(100% - 40px);
	}
	.list-scroll-content{
		height: 100%;
	}
	
	.navbar{
		display: flex;
		height: 40px;
		padding: 0 5px;
		background: #fff;
		box-shadow: 0 1px 5px rgba(0,0,0,.06);
		position: relative;
		z-index: 10;
		.nav-item{
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 15px;
			color: #333;
			position: relative;
			&.current{
				color: #fe124a;
				&:after{
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 44px;
					height: 0;
					border-bottom: 2px solid #fe124a;
				}
			}
		}
	}

	.uni-swiper-item{
		height: auto;
	}
	.order-item{
		display: flex;
		flex-direction: column;
		padding-left: 30upx;
		background: #fff;
		margin-top: 16upx;
		.i-top{
			display: flex;
			justify-content:space-between;
			align-items:flex-start;
			padding-right:30upx;
			font-size: 28rpx;
			color: #333;
			position: relative;
			.tl{
				flex:1;
				padding:20rpx 0 10rpx 0;
				.num{
					font-size:26rpx;
					color:#333;
					margin-bottom:5rpx;
				}
				.time{
					font-size:26rpx;
					color:#999;
				}
			}
			
			.state{
				color: #fe124a;
				margin:15rpx 0 0 0;
			}
			.del-btn{
				padding: 10upx 0 10upx 36upx;
				font-size: 32rpx;
				color: #ccc;
				position: relative;
				&:after{
					content: '';
					width: 0;
					height: 30upx;
					border-left: 1px solid #ccc;
					position: absolute;
					left: 20upx;
					top: 50%;
					transform: translateY(-50%);
				}
			}
		}
		/* 多条商品 */
		.goods-box{
			height: 160upx;
			padding: 20upx 0;
			white-space: nowrap;
			.goods-item{
				width: 120upx;
				height: 120upx;
				display: inline-block;
				margin-right: 24upx;
			}
			.goods-img{
				display: block;
				width: 100%;
				height: 100%;
			}
		}
		/* 单条商品 */
		.goods-box-single{
			display: flex;
			padding: 20upx 0;
			.goods-img{
				display: block;
				width: 120upx;
				height: 120upx;
			}
			.right{
				flex: 1;
				display: flex;
				flex-direction: column;
				padding: 0 30upx 0 24upx;
				overflow: hidden;
				.title{
					font-size: 28rpx;
					color: #333;
					line-height: 1;
				}
				.attr-box{
					font-size: 26rpx;
					color: #ccc;
					padding: 10upx 12upx;
				}
				.price{
					font-size: 28rpx;
					color: #333;
					&:before{
						content: '￥';
						font-size: 24rpx;
						margin: 0 2upx 0 8upx;
					}
				}
			}
		}
		
		.price-box{
			display: flex;
			justify-content: flex-end;
			align-items: baseline;
			padding: 20upx 30upx;
			font-size:26rpx;
			color: #ccc;
			.num{
				margin: 0 8upx;
				color: #333;
			}
			.price{
				font-size:32rpx;
				color: #333;
				&:before{
					content: '￥';
					font-size:24rpx;
					margin: 0 2upx 0 8upx;
				}
			}
		}
		.action-box{
			display: flex;
			justify-content: flex-end;
			align-items: center;
			height: 100upx;
			position: relative;
			padding-right: 30upx;
		}
		.action-btn{
			width: 160upx;
			height: 60upx;
			margin: 0;
			margin-left: 24upx;
			padding: 0;
			text-align: center;
			line-height: 60upx;
			font-size:26rpx;
			color: #333;
			background: #fff;
			border-radius: 100px;
			&:after{
				border-radius: 100px;
			}
			&.recom{
				background: #fff9f9;
				color: #fe124a;
				&:after{
					border-color: #f7bcc8;
				}
			}
		}
	}
	
	.loadmore{
		height: 60rpx;
		padding:30rpx 0;
	}
	/deep/ .u-loadmore__content__text[data-v-055cbf89]{
		height: 20px;
		padding-top: 10px;
	}
	
</style>